{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "from typing_extensions import TypedDict\n",
    "from langchain_core.runnables import RunnableLambda"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### as_tool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Args(TypedDict):\n",
    "    a: int\n",
    "    b: List[int]\n",
    "\n",
    "def f(x: Args) -> str:\n",
    "    return str(x[\"a\"] * max(x[\"b\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RunnableLambda(f)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "runnable = RunnableLambda(f)\n",
    "runnable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'RunnableLambda' object has no attribute 'as_tool'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[8], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m as_tool \u001b[38;5;241m=\u001b[39m \u001b[43mrunnable\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mas_tool\u001b[49m()\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'RunnableLambda' object has no attribute 'as_tool'"
     ]
    }
   ],
   "source": [
    "#测试版本才有此属性\n",
    "as_tool = runnable.as_tool()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'as_tool' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[11], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mas_tool\u001b[49m\u001b[38;5;241m.\u001b[39minvoke({\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;241m3\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m: [\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m]})\n",
      "\u001b[1;31mNameError\u001b[0m: name 'as_tool' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "as_tool.invoke({\"a\": 3, \"b\": [1, 2]})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### astream_events"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'event': 'on_chain_start',\n",
       "  'data': {'input': 'hello'},\n",
       "  'name': 'reverse',\n",
       "  'tags': [],\n",
       "  'run_id': 'eb7aba8e-d00d-49c4-880d-0597aa451fc0',\n",
       "  'metadata': {},\n",
       "  'parent_ids': []},\n",
       " {'event': 'on_chain_stream',\n",
       "  'run_id': 'eb7aba8e-d00d-49c4-880d-0597aa451fc0',\n",
       "  'name': 'reverse',\n",
       "  'tags': [],\n",
       "  'metadata': {},\n",
       "  'data': {'chunk': 'olleh'},\n",
       "  'parent_ids': []},\n",
       " {'event': 'on_chain_end',\n",
       "  'data': {'output': 'olleh'},\n",
       "  'run_id': 'eb7aba8e-d00d-49c4-880d-0597aa451fc0',\n",
       "  'name': 'reverse',\n",
       "  'tags': [],\n",
       "  'metadata': {},\n",
       "  'parent_ids': []}]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.runnables import RunnableLambda\n",
    "\n",
    "async def reverse(s: str) -> str:\n",
    "    return s[::-1]\n",
    "\n",
    "chain = RunnableLambda(func=reverse)\n",
    "\n",
    "events = [\n",
    "    event async for event in chain.astream_events(\"hello\", version=\"v2\")\n",
    "]\n",
    "events"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
